package com.ruoyi.pos.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.pos.domain.OrderItem;
import com.ruoyi.pos.mapper.GoodsMapper;
import com.ruoyi.pos.domain.Goods;
import com.ruoyi.pos.service.IGoodsService;

/**
 * goodsService业务层处理
 * 
 * @author wzh
 * @date 2021-04-19
 */
@Service
public class GoodsServiceImpl implements IGoodsService 
{
    @Autowired
    private GoodsMapper goodsMapper;

    /**
     * 查询goods
     * 
     * @param id goodsID
     * @return goods
     */
    @Override
    public Goods selectGoodsById(Long id)
    {
        return goodsMapper.selectGoodsById(id);
    }

    @Override
    public Goods selectGoodsByCode(String barcode) { return goodsMapper.selectGoodsByCode(barcode); }

    /**
     * 查询goods列表
     * 
     * @param goods goods
     * @return goods
     */
    @Override
    public List<Goods> selectGoodsList(Goods goods)
    {
        return goodsMapper.selectGoodsList(goods);
    }

    /**
     * 新增goods
     * 
     * @param goods goods
     * @return 结果
     */
    @Transactional
    @Override
    public int insertGoods(Goods goods)
    {
        goods.setCreateTime(DateUtils.getNowDate());
        int rows = goodsMapper.insertGoods(goods);
//        insertOrderItem(goods);
        return rows;
    }

    /**
     * 修改goods
     * 
     * @param goods goods
     * @return 结果
     */
    @Transactional
    @Override
    public int updateGoods(Goods goods)
    {
        goodsMapper.deleteOrderItemByGid(goods.getId());
//        insertOrderItem(goods);
        return goodsMapper.updateGoods(goods);
    }

    /**
     * 批量删除goods
     * 
     * @param ids 需要删除的goodsID
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteGoodsByIds(Long[] ids)
    {
        goodsMapper.deleteOrderItemByGids(ids);
        return goodsMapper.deleteGoodsByIds(ids);
    }

    /**
     * 删除goods信息
     * 
     * @param id goodsID
     * @return 结果
     */
    @Override
    public int deleteGoodsById(Long id)
    {
        goodsMapper.deleteOrderItemByGid(id);
        return goodsMapper.deleteGoodsById(id);
    }

//    /**
//     * 新增item信息
//     *
//     * @param goods goods对象
//     */
//    public void insertOrderItem(Goods goods)
//    {
//        List<OrderItem> orderItemList = goods.getOrderItemList();
//        Long id = goods.getId();
//        if (StringUtils.isNotNull(orderItemList))
//        {
//            List<OrderItem> list = new ArrayList<OrderItem>();
//            for (OrderItem orderItem : orderItemList)
//            {
//                orderItem.setGid(id);
//                list.add(orderItem);
//            }
//            if (list.size() > 0)
//            {
//                goodsMapper.batchOrderItem(list);
//            }
//        }
//    }
}
